package com.drw.start.oauth2.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.springframework.security.core.GrantedAuthority;

@Entity
@Table(name="authority", uniqueConstraints={@UniqueConstraint(columnNames = {"user_id" , "authority"})})
public class Authority implements GrantedAuthority {

    /**
     *
     */
    private static final long serialVersionUID = 6534950615400310162L;

    @Id
    @Column(name="id", nullable = false, unique=true, columnDefinition="BIGINT COMMENT 'id值'")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long  id;

    @Column(nullable = false, columnDefinition="VARCHAR(45) COMMENT '权限角色'")
    @Enumerated(EnumType.STRING)
    private Role authority;

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "user_id", referencedColumnName="id", nullable=false)
    private Users users;


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Users getUsers() {
        return users;
    }

    public void setUsers(Users users) {
        this.users = users;
    }

    public void setAuthority(Role authority) {
        this.authority = authority;
    }

    @Override
    public String getAuthority() {
        return authority.toString();
    }

}
